<p>When testing exception via <code>@Test</code> annotation, having additional assertions inside that test method can be problematic because any code
after the raised exception will not be executed. It will prevent you to test the state of the program after the raised exception and, at worst, make
you&nbsp;misleadingly think that it is executed.</p>
<p>You should consider moving any assertions into a separate test method where possible, or using <a
href="https://github.com/junit-team/junit4/wiki/Exception-testing#using-assertthrows-method">org.junit.Assert.assertThrows</a> instead.</p>
<p>Alternatively, you could use <a href="https://github.com/junit-team/junit4/wiki/Exception-testing#trycatch-idiom">try-catch idiom</a> for JUnit
version &lt; 4.13 or if your project does not support lambdas.</p>
<h2>Noncompliant Code Example</h2>
<pre>
@Test(expected = IndexOutOfBoundsException.class)
public void testShouldFail() {
  get();
  // This test pass since execution will never get past this line.
  Assert.assertEquals(0, 1);
}

private Object get() {
  throw new IndexOutOfBoundsException();
}
</pre>
<h2>Compliant Solution</h2>
<ul>
  <li> For JUnit &gt;= 4.13, use <a
  href="https://github.com/junit-team/junit4/wiki/Exception-testing#using-assertthrows-method">org.junit.Assert.assertThrows</a>: </li>
</ul>
<pre>
// This test correctly fails.
@Test
public void testToString() {
    Object obj = get();
    Assert.assertThrows(IndexOutOfBoundsException.class, () -&gt; obj.toString());
    Assert.assertEquals(0, 1);
}
</pre>
<ul>
  <li> For JUnit &lt; 4.13, use the <a href="https://github.com/junit-team/junit4/wiki/Exception-testing#trycatch-idiom">try-catch idiom</a>: </li>
</ul>
<pre>
@Test
public void testShouldFail() {
    Object obj = get();
    try {
        obj.toString();
        Assert.fail("Expected an IndexOutOfBoundsException to be thrown");
    } catch (IndexOutOfBoundsException e) {}
    Assert.assertEquals(0, 1); // Correctly fails.
}
</pre>
<h2>See</h2>
<ul>
  <li> <a href="https://github.com/junit-team/junit4/wiki/Exception-testing">JUnit exception testing documentation</a> </li>
</ul>

